package com.qxzn.app.util;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import static android.provider.MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE;
import static android.provider.MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;

public class FileUtil {

    public static final String baseApkPath = "apk";
    public static final String baseFilePath = "file";
    public static final String baseAudioPath = "audio";
    public static final String baseImagePath = "image";
    public static final String basePhotoPath = "photo";
    public static final String baseCompressPath = "compress";
    public static final String baseCropPath = "crop";

    public static String uriToStrPath(Context context, Uri uri) {
        String path = null;
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            }
            cursor.close();
        }
        return path;
    }

    /**
     * APK下载文件路径
     *
     * @param context
     * @return
     */
    public static String getBaseApkPath(Context context) {
        return context.getExternalFilesDir(baseApkPath).getAbsolutePath();
    }

    /**
     * 存储文件路径
     *
     * @param context
     * @return
     */
    public static String getBaseFilePath(Context context) {
        return context.getExternalFilesDir(baseFilePath).getAbsolutePath();
    }

    /**
     * 存储音频路径
     *
     * @param context
     * @return
     */
    public static String getBaseAudioPath(Context context) {
        return context.getExternalFilesDir(baseAudioPath).getAbsolutePath();
    }

    /**
     * 存储图片路径
     *
     * @param context
     * @return
     */
    public static String getBaseImagePath(Context context) {
        return context.getExternalFilesDir(baseImagePath).getAbsolutePath();
    }

    /**
     * 存储拍照图片路径
     *
     * @param context
     * @return
     */
    public static String getBasePhotoPath(Context context) {
        return context.getExternalFilesDir(basePhotoPath).getAbsolutePath();
    }

    /**
     * 压缩后的图片路径
     *
     * @param context
     * @return
     */
    public static String getBaseCropPath(Context context) {
        return context.getExternalFilesDir(baseCropPath).getAbsolutePath();
    }

    public static boolean deleteFile(File file) {
        if (file != null && file.exists() && !file.isDirectory()) {
            file.delete();
            return true;
        }
        return false;
    }

    /**
     * 获取文件夹所占用的存储空间的大小
     *
     * @param file
     * @return
     */
    public static int getDirectoryFileSize(File file) {
        int size = 0;
        if (file != null && file.exists()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File subFile : files) {
                    if (subFile.isDirectory()) {
                        size += getDirectoryFileSize(subFile);
                    } else {
                        size += subFile.length();
                    }
                }
            }
        }
        return size;
    }

    public static File getOutputMediaFile(int type, String filePath) {
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.

//        File mediaStorageDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), FILE_PATH);
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            AppUtil.showLog("can not get sdcard!");
            return null;
        }
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), filePath);
//        File mediaStorageDir = new File(Environment.getExternalStorageDirectory().getPath());
        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            AppUtil.showLog("mkdirs: " + mediaStorageDir.getPath());
            if (!mediaStorageDir.mkdirs()) {
                AppUtil.showLog("failed to create directory");
                return null;
            }
        } else {
            AppUtil.showLog("mkdirs,文件夹已存在： " + mediaStorageDir.getPath());
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
        } else if (type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
        } else {
            return null;
        }
//        try {
//            Log.d(TAG, "mediaFile.createNewFile(): " + mediaFile.createNewFile());
//        } catch (IOException e) {
//            Log.e(TAG, "createNewFile IOException: " + e);
//            e.printStackTrace();
//        }
        return mediaFile;
    }

}
